package gavdi.dk.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.json.JSONException;
import org.json.JSONObject;


import Exceptions.ConverterException;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

public class Tools {
	
	public static JSONObject createLoginJSON(String UserID) throws ConverterException

	{
		// Laver et JSON Object
		JSONObject jsonObj = new JSONObject();
		JSONObject header = new JSONObject();
		try {
			// Tilføjer informationtil JSON objektet
			jsonObj.put("id", UserID);
			header.put("cmd", "L");
			jsonObj.put("header", header);		
		} 
		catch (JSONException e) {
			e.printStackTrace();
			throw new ConverterException("ConverterException/JSONException - createLoginJSON:" + e.getMessage());
		}
		return jsonObj;
	}	

	public static JSONObject createTimeRegJSON(String UserID, String wbs, String date, String hours) throws ConverterException{
		JSONObject jsonObjSend = new JSONObject();                              
		try {
			jsonObjSend.put("id", UserID);
			jsonObjSend.put("wbs", wbs);
			jsonObjSend.put("date", date);
			jsonObjSend.put("time", hours);
			                                                                                                                    
			// Add a nested JSONObject (e.g. for header information)                               
			JSONObject header = new JSONObject();                         
			header.put("cmd", "R");              
			// Security key. En sikkerhedsforanstaltning for, at der kaldes fra den korrekte device.                              
			header.put("key","1234567890"); 
			// Device OS version                              
			header.put("language", "es-es");     
			// Language of the Android client                             
			jsonObjSend.put("header", header);  
		} catch (JSONException e) {
			e.printStackTrace();
			throw new ConverterException("ConverterException/JSONException - createTimeRegJSON:" + e.getMessage());
		}
		return jsonObjSend;  
	}

	public static String convertStreamToString(InputStream is) throws ConverterException {
		String line = "";
		StringBuilder total = new StringBuilder();
		BufferedReader rd = new BufferedReader(new InputStreamReader(is));
		try {
			while ((line = rd.readLine()) != null) {
				total.append(line);
				Log.i("", line);
			}
		} 
		catch (Exception e) {
			e.printStackTrace();
			throw new ConverterException("ConverterException/JSONException - convertStreamToString:" + e.getMessage());
		}
		return total.toString();
	}
	
	public static String dateSplitter(String date){


		String dateSplitted = "";
		dateSplitted += date.substring(0,2) + "-";
		dateSplitted += date.substring(2,4) + "-";
		dateSplitted += date.substring(4);		

		return dateSplitted;
	}
	
	public static String dateRemovedSplitter(String date){

		return date.replaceAll("-", "");
	}
	
	public static String getDate(int daysToGoBack, boolean includeNameOfDay){
	    Calendar c = Calendar.getInstance();
	    SimpleDateFormat df;
	    String formattedDate = "";
	    
	    // This will add the name of day to the formattedDate String
	    // This is used for the buttons on MenuActivty, to show the proper date
	    c.add(Calendar.DATE, - daysToGoBack);
	    if (includeNameOfDay)
	    {
	    	df = new SimpleDateFormat("EEEE");
	    	formattedDate += df.format(c.getTime()) + "\n"; 
	    	df = new SimpleDateFormat("dd-MM-yyyy");
	    	formattedDate += df.format(c.getTime());
	    	 return formattedDate;
	    }
	
	    // This will format the date string to "Day-Month-Year" 
	    	df = new SimpleDateFormat("dd-MM-yyyy");
	    	formattedDate += df.format(c.getTime()); 
	    
        System.out.println(formattedDate);
        return formattedDate;
        
	}
	
	
	public static final boolean isInternetOn(Context context) {
		 
		ConnectivityManager connec =  (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
		 
		// ARE WE CONNECTED TO THE NET
		 
		if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED ||
		 
		connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING ||
		 
		connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING ||
		 
		connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED ) {
		 
		// MESSAGE TO SCREEN FOR TESTING (IF REQ)
		 
		//Toast.makeText(this, connectionType + ” connected”, Toast.LENGTH_SHORT).show();
		 
		return true;
		 
		} else if ( connec.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED ||  connec.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED  ) {
		 
		//System.out.println(“Not Connected”);
		 
		return false;
		 
		}
		 
		return false;
		 
		}
	


}
